package com.aqielife.netty.simple.handler;


import com.alibaba.fastjson2.JSON;
import com.aqielife.netty.simple.Response;
import com.aqielife.netty.simple.command.Command;
import com.aqielife.netty.simple.ImServer;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ConnectionHandler {
    public static void execute(ChannelHandlerContext ctx, Command command){
        if (ImServer.USERS.containsKey(command.getNickname())) {
            ctx.channel().writeAndFlush(Response.ok("用户昵称重复"));
            ctx.disconnect();
            return;
        }
        ImServer.USERS.put(command.getNickname(), ctx.channel());
        ctx.channel().writeAndFlush(Response.ok("connect success"));
        log.info("{} connect success", command.getNickname());
        ctx.channel().writeAndFlush(Response.ok(JSON.toJSONString(ImServer.USERS.keySet())));
    }
}
